using FairyGUI;

namespace GameFramework.UI.FairyGUI
{
    public partial class AFairyGUIFormLogic
    {
        /// <summary>
        /// 界面的框架组件 约定节点名为frame的组件为框架组件
        /// </summary>
        protected GComponent Frame { get; private set; }

        /// <summary>
        /// 界面的关闭按钮 当它被点击时会调用OnCloseButtonClick方法 约定框架组件下名为closeButton的组件为默认的关闭按钮
        /// </summary>
        protected GObject CloseButton
        {
            get => m_CloseButton;
            set
            {
                if (m_CloseButton == value)
                    return;
                
                if (m_CloseButton != null)
                    m_CloseButton.onClick.Remove(OnCloseButtonClick);

                m_CloseButton = value;
                
                if (m_CloseButton != null)
                    m_CloseButton.onClick.Add(OnCloseButtonClick);
            }
        }

        private GObject m_CloseButton;

        private void _OnInitFormFrame()
        {
            Frame = ContentPane.GetChild("frame") as GComponent;
            if (Frame != null)
            {
                CloseButton = Frame.GetChild("closeButton");
            }
        }

        private void _OnRecycleFormFrame()
        {
            CloseButton = null;
            Frame = null;
        }

        /// <summary>
        /// 关闭按钮被点击时调用 默认实现为关闭界面
        /// </summary>
        protected virtual void OnCloseButtonClick(EventContext context)
        {
            CloseForm();
        }
    }
}